
package system.base;

import java.sql.ResultSet;
import java.sql.SQLException;

import framework.core.*;
import system.CustomerStaffSearchEventEx;
import system.data.*;
/*
	Author    :Wing
	CreateTime:2008-02-02 12:33:55
*/
public class CustomerStaffSrv extends IHandler
{

	public CustomerStaffSrv() 
	{
		super();
	}
	private void deleteCustomerStaff(DBConnection con,SrvContext context) throws Exception
	{
		String[] ids = context.getParameterValues("ids");
		CustomerStaff obj = null;
		for(int i=0;i<ids.length;i++)
		{
			obj = CustomerStaff.fetch(con,ids[i]);
			if(obj!=null) obj.delete(con);
		}
	}
	private void saveCustomerStaff(DBConnection con,SrvContext context,ILoginContext loginContext) throws Exception
	{
		String id = context.getString("Id",null);
		CustomerStaff obj;
		CustomerStaffPageData data = (CustomerStaffPageData)PageData.fromForm(context,CustomerStaffPageData.class);
		if((id==null)||(id.equals("")))   //add
		{
			obj = CustomerStaff.fetch();
			data.setCreateUserId(loginContext.getLoginUserId());
            data.setCreateUser(loginContext.getLoginUserName());
    	}
    	else                     //update
    	{
    		obj = CustomerStaff.fetch(con,id);
    	}
		data.setLastUpdateUserId(loginContext.getLoginUserId());
        data.setLastUpdateUser(loginContext.getLoginUserName());
		context.setValue("CustomerStaffPageData",data);
		obj.fill(data);
		obj.save(con);
	}
	private void listCustomerStaff(DBConnection con,SrvContext context,String path) throws Exception
	{
		CustomerStaffSearchEventEx event = (CustomerStaffSearchEventEx)SearchEvent.fromForm(context,CustomerStaffSearchEventEx.class);
		event.setCustomerCode(((Customer)context.getSessionValue("CustomerDetail")).getCode());
		event.setCustomer_Name(((Customer)context.getSessionValue("CustomerDetail")).getName());
		context.setSessionValue("CustomerStaffSearchEvent",event);
		context.setNextUrl(path+"CustomerStaffList.jsp");
	}
	private void backCustomerStaff(DBConnection con,SrvContext context,String path) throws Exception
	{
		context.setNextUrl(path+"CustomerStaffList.jsp");
	}
	
	public void doAction(DBConnection con,SrvContext context,ILoginContext loginContext) throws Exception
	{
		String action_code = context.getActionCode();
		String path = "/system/base/";
		
		if(action_code.equalsIgnoreCase("listCustomerStaff"))
		{
		    context.setErrorUrl(path+"CustomerList.jsp");
		    String id = context.getString("CustomerID",null);
	        if(id != null){
	            Customer obj = Customer.fetch(con,id);
	            context.setSessionValue("CustomerDetail",obj);
	            if(!obj.getWarehouseCode().equals(loginContext.getLoginWarehouseCode()))
	                throw new BizException("您无权进行该操作!");
	        } 
			listCustomerStaff(con,context,path);
			return;
		}
		if(action_code.equalsIgnoreCase("viewCustomerStaff"))
		{
			String id = context.getString("Id",null);
			CustomerStaff obj = CustomerStaff.fetch(con,id);
			//add by benny begin
			String sql = "select name from t_customer where code = '"+obj.getCustomerCode()+"'";
            DBResult dbrs = null;
            ResultSet rs = null;
            String result = "";
            try {
                dbrs = DBEngine.doSelect(con, sql);
                rs = dbrs.getRs();
                if (rs.next()) {
                    result = rs.getString(1);
                }
            } catch (SQLException e) {
                
                e.printStackTrace();
            } catch (Exception e) {
                
                e.printStackTrace();
            } finally {
                try {
                    dbrs.clear();
                } catch (Exception e) {
                    
                    e.printStackTrace();
                }
            }
			//add by benny end
			context.setValue("CustomerStaff",obj);
			context.setSessionValue("staffCustometName", result);
			context.setNextUrl(path+"CustomerStaffView.jsp");
			return;
		}
		if(action_code.equalsIgnoreCase("deleteCustomerStaff"))
		{
			context.setErrorUrl(path+"CustomerStaffList.jsp");
			deleteCustomerStaff(con,context);
			backCustomerStaff(con,context,path);
			return;
		}
			
		if(action_code.equalsIgnoreCase("toAddCustomerStaff"))
		{
			CustomerStaff obj = CustomerStaff.fetch();
			CustomerStaffPageData data = (CustomerStaffPageData)PageData.fromTable(obj,CustomerStaffPageData.class);
			context.setValue("CustomerStaffPageData",data);
			String action = "add";
			context.setSessionValue("ActionValue", action);
			context.setNextUrl(path+"CustomerStaffEdit.jsp");
			return;
		}
		if(action_code.equalsIgnoreCase("toUpdateCustomerStaff"))
		{
			String id = context.getString("Id",null);
			CustomerStaff obj = CustomerStaff.fetch(con,id);
			CustomerStaffPageData data = (CustomerStaffPageData)PageData.fromTable(obj,CustomerStaffPageData.class);
			context.setValue("CustomerStaffPageData",data);
			String action = "update";
            context.setSessionValue("ActionValue", action);
			context.setNextUrl(path+"CustomerStaffEdit.jsp");
			return;
		}
		if(action_code.equalsIgnoreCase("toCopyCustomerStaff"))
		{
			String id = context.getString("Id",null);
			CustomerStaff obj = CustomerStaff.copy(CustomerStaff.fetch(con,id));
			CustomerStaffPageData data = (CustomerStaffPageData)PageData.fromTable(obj,CustomerStaffPageData.class);
			context.setValue("CustomerStaffPageData",data);
			context.setNextUrl(path+"CustomerStaffEdit.jsp");
			return;
		}
		if(action_code.equalsIgnoreCase("saveCustomerStaff"))
		{
			context.setErrorUrl(path+"CustomerStaffEdit.jsp");
			saveCustomerStaff(con,context,loginContext);
			backCustomerStaff(con,context,path);
			return;
		}
		if(action_code.equalsIgnoreCase("saveAndNewCustomerStaff"))
		{
			context.setErrorUrl(path+"CustomerStaffEdit.jsp");
			saveCustomerStaff(con,context,loginContext);
			CustomerStaff obj = CustomerStaff.fetch();
			CustomerStaffPageData data = (CustomerStaffPageData)PageData.fromTable(obj,CustomerStaffPageData.class);
			context.setValue("CustomerStaffPageData",data);
			context.setNextUrl(path+"CustomerStaffEdit.jsp");			
			return;
		}
		if(action_code.equalsIgnoreCase("backListCustomerStaff"))
		{
			backCustomerStaff(con,context,path);
			return;
		}
		
		//add by Wing start

		//add by Wing end

		Logger.throwException("CustomerStaffSrv","unknown action_code="+action_code);
	}
}
